package com.macro.mall.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.IdWorker;
import com.macro.mall.common.util.UUIDUtil;
import com.macro.mall.component.FileTemplate;
import com.macro.mall.dto.e.SysPictureEntDTO;
import com.macro.mall.mapper.BasicKeyImgMapper;
import com.macro.mall.model.BasicKeyImg;
import com.macro.mall.pojo.BasicKeyImgVo;
import com.macro.mall.query.PictureQuery;
import com.macro.mall.service.SysPictureService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
public class SysPictureServiceImpl implements SysPictureService {

    @Autowired
    private BasicKeyImgMapper basicKeyImgMapper;

    @Autowired
    private FileTemplate fileTemplate;

    @Autowired
    private IdWorker idWorker;


    /***
     * 新增 图片配置
     * @param img
     * @param file
     * @return
     */
    @Transactional
    @Override
    public CommonResult edit(BasicKeyImg img, MultipartFile file) {
        try {
            if (file != null && file.getSize() > 0) {
                String fileUrl = fileTemplate.upload(file);
                img.setImgPath(fileUrl);
            }
            if (img.getKeyList() != null && !img.getKeyList().equals("")) {
                String[] split = img.getKeyList().split(",");
                List<String> list = Arrays.asList(split);
                String jsonString = JSON.toJSONString(list);
                img.setKeyList(jsonString);
            }
            if (img.getId() == null || img.getId() <= 0) {
                img.setId(idWorker.nextId());
                img.setCreatedDt(new Date());
                img.setUpdatedDt(new Date());
                img.setImgId(UUIDUtil.getUUid());
                int row = basicKeyImgMapper.insertSelective(img);
                return result(row, "添加失败");
            }
            img.setUpdatedDt(new Date());
            int row = basicKeyImgMapper.updateByPrimaryKeySelective(img);
            return result(row, "修改失败");
        } catch (Exception e) {
            System.out.println("err： " + e.getMessage());
            return CommonResult.failed(e.getMessage());

        }

    }


    /***
     * 删除 图片配置
     * @param id
     * @return
     */
    @Override
    public CommonResult delete(Long id) {
        int row = basicKeyImgMapper.deleteByPrimaryKey(id);
        return result(row, "添加失败");
    }


    @Override
    public CommonResult list(SysPictureEntDTO sysPictureEntDTO) {
        Map<String, Object> result = new HashMap<>();
        PageHelper.startPage(sysPictureEntDTO.getPage(), sysPictureEntDTO.getPageSize());
        PictureQuery query = new PictureQuery();
        BeanUtils.copyProperties(sysPictureEntDTO, query);
        List<BasicKeyImg> list = basicKeyImgMapper.list(query);
        List<BasicKeyImgVo> resultVo = new ArrayList<>(list.size());

        for (BasicKeyImg basicKeyImg : list) {
            BasicKeyImgVo vo = new BasicKeyImgVo();
            BeanUtils.copyProperties(basicKeyImg, vo);
            if (basicKeyImg.getKeyList() != null && !basicKeyImg.getKeyList().equals("") && basicKeyImg.getKeyList().length() > 0) {
                List<String> list1 = JSON.parseArray(basicKeyImg.getKeyList(), String.class);
                vo.setKeyList(list1);
            }
            resultVo.add(vo);
        }


        PageInfo<BasicKeyImgVo> pageInfo = new PageInfo<>(resultVo);
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return CommonResult.success(result);
    }

    @Override
    public int modify(SysPictureEntDTO sysPictureEntDTO) {
        BasicKeyImg basicKeyImg = new BasicKeyImg();
        basicKeyImg.setId(sysPictureEntDTO.getId());
        basicKeyImg.setStatus(sysPictureEntDTO.getStatus());
        basicKeyImg.setImgName(sysPictureEntDTO.getPictureName());
        return basicKeyImgMapper.updateByPrimaryKeySelective(basicKeyImg);
    }


    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private CommonResult result(int row, String msg) {
        if (row > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed(msg);
    }

}
